﻿using System.Collections.Generic;

namespace BigfootMVC.Helpers
{
    /// <summary>
    /// This is SelectItem list builder. It aids in the fluid creation of a collection of SelectItems
    /// </summary>
    public class SelectItemList
    {

        private List<SelectItem> _items = new List<SelectItem>();

        public SelectItemList(){}

        public SelectItemList(IEnumerable<SelectItem> items)
        {
            _items.AddRange(items);
        }

        /// <summary>
        /// Insers a certain value at the top of the list
        /// </summary>
        public SelectItemList InsertTop(string value, string text)
        {
            _items.Insert(0, new SelectItem(text, value));
            return this;
        }

        /// <summary>
        /// Insers a certain value at the top of the list
        /// </summary>
        public SelectItemList InsertCaption(string text)
        {
            return InsertTop("-1", text);
        }

        /// <summary>
        /// Adds a list item where the text and value is the same
        /// </summary>
        /// <param name="text">Text to use for the list item. Will be used for the value as well</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string text)
        {
            _items.Add(new SelectItem(text));
            return this;
        }

        /// <summary>
        /// Adds a list item where the text and value might be different
        /// </summary>
        /// <param name="value">The value to assign to the list item</param>
        /// <param name="text">Text to use for the list item.</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string value, string text)
        {
            _items.Add(new SelectItem(text, value));
            return this;
        }

        /// <summary>
        /// Adds a list item where the text and value might be different
        /// </summary>
        /// <param name="value">The value to assign to the list item</param>
        /// <param name="text">Text to use for the list item.</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(int value, string text)
        {
            _items.Add(new SelectItem(text, value.ToString()));
            return this;
        }

        /// <summary>
        /// Adds a list item where the text and value might be different and you can specify wether the item is selected
        /// </summary>
        /// <param name="value">The value to assign to the list item</param>
        /// <param name="text">Text to use for the list item.</param>
        /// <param name="selected">Determines if the item is selected</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(string value, string text, bool selected)
        {
            _items.Add(new SelectItem(text, value, selected));
            return this;
        }

        /// <summary>
        /// Adds a list of ListItems where the text and value is the same using an array of texts
        /// </summary>
        /// <param name="itemtexts">Array of texts to use when creating the List items. Will be used for the value as well</param>
        /// <returns>Retruns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(params string[] itemtexts)
        {
            foreach (var t in itemtexts)
            {
                _items.Add(new SelectItem(t));
            }
            return this;
        }

        /// <summary>
        /// Adds a list of SelectItems
        /// </summary>
        /// <param name="items">Array of SelectItem to add</param>
        /// <returns>Returns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(IEnumerable<SelectItem> items)
        {
            _items.AddRange(items);
            return this;
        }

        /// <summary>
        /// Adds a SelectItem
        /// </summary>
        /// <param name="item">SelectItem</param>
        /// <returns>Returns the select item list to aid in the fluid interface</returns>
        public SelectItemList Add(SelectItem item)
        {
            _items.Add(item);
            return this;
        }

        /// <summary>
        /// Returns the created list of items
        /// </summary>
        public List<SelectItem> ToList()
        {
            return _items;
        }

        /// <summary>
        /// Returns an array of SelectItem
        /// </summary>
        public SelectItem[] ToArray()
        {
            return _items.ToArray();
        }

    }
}
